// pages/orderPay/orderPay.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '确认订单', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    isIphoneX: false, //是否是iphoneX
    userAddressData:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      goods:{
        good_name:options.good_name,
        id:options.id,
        image:options.image,
        market_price:options.market_price,
        pv:options.pv
      }
    })
  },
  onShow() {
    var _this=this;
    //判断iphone x
    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          _this.setData({
            isIphoneX: true
          })
        }
      }
    })
    if (wx.getStorageSync('address')) {
      var address = wx.getStorageSync('address');
      _this.setData({
        userAddressData:{
          buyer_name: address.buyer_name,
          buyer_mobile: address.buyer_mobile,
          province_id: address.province_id,
          city_id: address.city_id,
          area_id: address.area_id,
          address: address.address,
          addressSHQ: address.addressSHQ
        }
        
      })
    }
    


    



  },
  //选择地址
  onChoiceAddress(e) {
    var _this = this;
    wx.navigateTo({
      url: '/pages/address/modify/modify?jf=1',
    })
    wx.setStorageSync('goods', _this.data.goods)
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  //确认兑换
  onConfirmOder(){
    var _this=this;
    var buy_goods = [];
    buy_goods = buy_goods.concat({
      "good_id": _this.data.goods.id,
      "num": 1
    })
    buy_goods = JSON.stringify(buy_goods)
    if(_this.data.userAddressData==''){
      wx.showToast({
        title: '收货地址不能为空',
        icon: 'none',
        duration: 2000
      })
    }else{
      _this.setData({
        loadModal: true
      })
      $api.confirmJforder({
        login_token:wx.getStorageSync('login_token'),
        buyer_name:_this.data.userAddressData.buyer_name,
        buyer_mobile:_this.data.userAddressData.buyer_mobile,
        province_id:_this.data.userAddressData.province_id,
        city_id:_this.data.userAddressData.city_id,
        area_id:_this.data.userAddressData.area_id,
        address:_this.data.userAddressData.address,
        buy_goods:buy_goods
      }).then((res)=>{
        _this.setData({
          loadModal:false
        })
        if(res.data.code==0){
          wx.removeStorageSync('address');
          wx.removeStorageSync('goods');
          wx.redirectTo({
            url: '/pages/order/order?type=4',
          })
        }
        


      }).catch((err)=>{
        _this.setData({
          loadModal:false
        })
      })

    }
    
   

  },
})